package com.ruoyi.report_jl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.BigDecimalUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import com.ruoyi.report_tg.domain.vo.YexVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_jl.domain.bo.ReportJlKfnewsxzssyjlbBsInfoBo;
import com.ruoyi.report_jl.domain.vo.ReportJlKfnewsxzssyjlbBsInfoVo;
import com.ruoyi.report_jl.domain.ReportJlKfnewsxzssyjlbBsInfo;
import com.ruoyi.report_jl.mapper.ReportJlKfnewsxzssyjlbBsInfoMapper;
import com.ruoyi.report_jl.service.IReportJlKfnewsxzssyjlbBsInfoService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

import static java.lang.Math.log10;

/**
 * 矿粉塑性指数试验检测记录Service业务层处理
 *
 * @author tll
 * @date 2024-07-16
 */
@RequiredArgsConstructor
@Service
public class ReportJlKfnewsxzssyjlbBsInfoServiceImpl implements IReportJlKfnewsxzssyjlbBsInfoService {

    private final ReportJlKfnewsxzssyjlbBsInfoMapper baseMapper;

    /**
     * 查询矿粉塑性指数试验检测记录
     */
    @Override
    @FIllBasicInfo
    public ReportJlKfnewsxzssyjlbBsInfoVo queryById(Long excelId){
        return baseMapper.selectVoById(excelId);
    }

    /**
     * 查询矿粉塑性指数试验检测记录列表
     */
    @Override
    public TableDataInfo<ReportJlKfnewsxzssyjlbBsInfoVo> queryPageList(ReportJlKfnewsxzssyjlbBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportJlKfnewsxzssyjlbBsInfo> lqw = buildQueryWrapper(bo);
        Page<ReportJlKfnewsxzssyjlbBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询矿粉塑性指数试验检测记录列表
     */
    @Override
    public List<ReportJlKfnewsxzssyjlbBsInfoVo> queryList(ReportJlKfnewsxzssyjlbBsInfoBo bo) {
        LambdaQueryWrapper<ReportJlKfnewsxzssyjlbBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportJlKfnewsxzssyjlbBsInfo> buildQueryWrapper(ReportJlKfnewsxzssyjlbBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportJlKfnewsxzssyjlbBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTotalPage() != null, ReportJlKfnewsxzssyjlbBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(bo.getPage() != null, ReportJlKfnewsxzssyjlbBsInfo::getPage, bo.getPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJilbh()), ReportJlKfnewsxzssyjlbBsInfo::getJilbh, bo.getJilbh());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), ReportJlKfnewsxzssyjlbBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), ReportJlKfnewsxzssyjlbBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), ReportJlKfnewsxzssyjlbBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), ReportJlKfnewsxzssyjlbBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), ReportJlKfnewsxzssyjlbBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), ReportJlKfnewsxzssyjlbBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(bo.getYangpsj() != null, ReportJlKfnewsxzssyjlbBsInfo::getYangpsj, bo.getYangpsj());
        lqw.eq(StringUtils.isNotBlank(bo.getShiyjcrq()), ReportJlKfnewsxzssyjlbBsInfo::getShiyjcrq, bo.getShiyjcrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjwd()), ReportJlKfnewsxzssyjlbBsInfo::getShiytjwd, bo.getShiytjwd());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjsd()), ReportJlKfnewsxzssyjlbBsInfo::getShiytjsd, bo.getShiytjsd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), ReportJlKfnewsxzssyjlbBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), ReportJlKfnewsxzssyjlbBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), ReportJlKfnewsxzssyjlbBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), ReportJlKfnewsxzssyjlbBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getBaobmc()), ReportJlKfnewsxzssyjlbBsInfo::getBaobmc, bo.getBaobmc());
        lqw.eq(StringUtils.isNotBlank(bo.getBaobbh()), ReportJlKfnewsxzssyjlbBsInfo::getBaobbh, bo.getBaobbh());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), ReportJlKfnewsxzssyjlbBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancr()), ReportJlKfnewsxzssyjlbBsInfo::getJiancr, bo.getJiancr());
        lqw.eq(StringUtils.isNotBlank(bo.getJilr()), ReportJlKfnewsxzssyjlbBsInfo::getJilr, bo.getJilr());
        lqw.eq(StringUtils.isNotBlank(bo.getFuhr()), ReportJlKfnewsxzssyjlbBsInfo::getFuhr, bo.getFuhr());
        lqw.eq(bo.getRiq() != null, ReportJlKfnewsxzssyjlbBsInfo::getRiq, bo.getRiq());
        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), ReportJlKfnewsxzssyjlbBsInfo::getShebIds, bo.getShebIds());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyjIds()), ReportJlKfnewsxzssyjlbBsInfo::getJiancyjIds, bo.getJiancyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyjIds()), ReportJlKfnewsxzssyjlbBsInfo::getPandyjIds, bo.getPandyjIds());
        lqw.eq(bo.getH11() != null, ReportJlKfnewsxzssyjlbBsInfo::getH11, bo.getH11());
        lqw.eq(bo.getH12() != null, ReportJlKfnewsxzssyjlbBsInfo::getH12, bo.getH12());
        lqw.eq(bo.getH13() != null, ReportJlKfnewsxzssyjlbBsInfo::getH13, bo.getH13());
        lqw.eq(bo.getH21() != null, ReportJlKfnewsxzssyjlbBsInfo::getH21, bo.getH21());
        lqw.eq(bo.getH22() != null, ReportJlKfnewsxzssyjlbBsInfo::getH22, bo.getH22());
        lqw.eq(bo.getH23() != null, ReportJlKfnewsxzssyjlbBsInfo::getH23, bo.getH23());
        lqw.eq(bo.getPingjh1() != null, ReportJlKfnewsxzssyjlbBsInfo::getPingjh1, bo.getPingjh1());
        lqw.eq(bo.getPingjh2() != null, ReportJlKfnewsxzssyjlbBsInfo::getPingjh2, bo.getPingjh2());
        lqw.eq(bo.getPingjh3() != null, ReportJlKfnewsxzssyjlbBsInfo::getPingjh3, bo.getPingjh3());
        lqw.eq(StringUtils.isNotBlank(bo.getHeh11()), ReportJlKfnewsxzssyjlbBsInfo::getHeh11, bo.getHeh11());
        lqw.eq(bo.getHezl11() != null, ReportJlKfnewsxzssyjlbBsInfo::getHezl11, bo.getHezl11());
        lqw.eq(bo.getHejstzl11() != null, ReportJlKfnewsxzssyjlbBsInfo::getHejstzl11, bo.getHejstzl11());
        lqw.eq(bo.getHejgtzl11() != null, ReportJlKfnewsxzssyjlbBsInfo::getHejgtzl11, bo.getHejgtzl11());
        lqw.eq(bo.getShuifzl11() != null, ReportJlKfnewsxzssyjlbBsInfo::getShuifzl11, bo.getShuifzl11());
        lqw.eq(bo.getGantzl11() != null, ReportJlKfnewsxzssyjlbBsInfo::getGantzl11, bo.getGantzl11());
        lqw.eq(bo.getHansl11() != null, ReportJlKfnewsxzssyjlbBsInfo::getHansl11, bo.getHansl11());
        lqw.eq(StringUtils.isNotBlank(bo.getHeh12()), ReportJlKfnewsxzssyjlbBsInfo::getHeh12, bo.getHeh12());
        lqw.eq(bo.getHezl12() != null, ReportJlKfnewsxzssyjlbBsInfo::getHezl12, bo.getHezl12());
        lqw.eq(bo.getHejstzl12() != null, ReportJlKfnewsxzssyjlbBsInfo::getHejstzl12, bo.getHejstzl12());
        lqw.eq(bo.getHejgtzl12() != null, ReportJlKfnewsxzssyjlbBsInfo::getHejgtzl12, bo.getHejgtzl12());
        lqw.eq(bo.getShuifzl12() != null, ReportJlKfnewsxzssyjlbBsInfo::getShuifzl12, bo.getShuifzl12());
        lqw.eq(bo.getGantzl12() != null, ReportJlKfnewsxzssyjlbBsInfo::getGantzl12, bo.getGantzl12());
        lqw.eq(bo.getHansl12() != null, ReportJlKfnewsxzssyjlbBsInfo::getHansl12, bo.getHansl12());
        lqw.eq(bo.getHanslpjz1() != null, ReportJlKfnewsxzssyjlbBsInfo::getHanslpjz1, bo.getHanslpjz1());
        lqw.eq(StringUtils.isNotBlank(bo.getHeh21()), ReportJlKfnewsxzssyjlbBsInfo::getHeh21, bo.getHeh21());
        lqw.eq(bo.getHezl21() != null, ReportJlKfnewsxzssyjlbBsInfo::getHezl21, bo.getHezl21());
        lqw.eq(bo.getHejstzl21() != null, ReportJlKfnewsxzssyjlbBsInfo::getHejstzl21, bo.getHejstzl21());
        lqw.eq(bo.getHejgtzl21() != null, ReportJlKfnewsxzssyjlbBsInfo::getHejgtzl21, bo.getHejgtzl21());
        lqw.eq(bo.getShuifzl21() != null, ReportJlKfnewsxzssyjlbBsInfo::getShuifzl21, bo.getShuifzl21());
        lqw.eq(bo.getGantzl21() != null, ReportJlKfnewsxzssyjlbBsInfo::getGantzl21, bo.getGantzl21());
        lqw.eq(bo.getHansl21() != null, ReportJlKfnewsxzssyjlbBsInfo::getHansl21, bo.getHansl21());
        lqw.eq(StringUtils.isNotBlank(bo.getHeh22()), ReportJlKfnewsxzssyjlbBsInfo::getHeh22, bo.getHeh22());
        lqw.eq(bo.getHezl22() != null, ReportJlKfnewsxzssyjlbBsInfo::getHezl22, bo.getHezl22());
        lqw.eq(bo.getHejstzl22() != null, ReportJlKfnewsxzssyjlbBsInfo::getHejstzl22, bo.getHejstzl22());
        lqw.eq(bo.getHejgtzl22() != null, ReportJlKfnewsxzssyjlbBsInfo::getHejgtzl22, bo.getHejgtzl22());
        lqw.eq(bo.getShuifzl22() != null, ReportJlKfnewsxzssyjlbBsInfo::getShuifzl22, bo.getShuifzl22());
        lqw.eq(bo.getGantzl22() != null, ReportJlKfnewsxzssyjlbBsInfo::getGantzl22, bo.getGantzl22());
        lqw.eq(bo.getHansl22() != null, ReportJlKfnewsxzssyjlbBsInfo::getHansl22, bo.getHansl22());
        lqw.eq(bo.getHanslpjz2() != null, ReportJlKfnewsxzssyjlbBsInfo::getHanslpjz2, bo.getHanslpjz2());
        lqw.eq(StringUtils.isNotBlank(bo.getHeh31()), ReportJlKfnewsxzssyjlbBsInfo::getHeh31, bo.getHeh31());
        lqw.eq(bo.getHezl31() != null, ReportJlKfnewsxzssyjlbBsInfo::getHezl31, bo.getHezl31());
        lqw.eq(bo.getHejstzl31() != null, ReportJlKfnewsxzssyjlbBsInfo::getHejstzl31, bo.getHejstzl31());
        lqw.eq(bo.getHejgtzl31() != null, ReportJlKfnewsxzssyjlbBsInfo::getHejgtzl31, bo.getHejgtzl31());
        lqw.eq(bo.getShuifzl31() != null, ReportJlKfnewsxzssyjlbBsInfo::getShuifzl31, bo.getShuifzl31());
        lqw.eq(bo.getGantzl31() != null, ReportJlKfnewsxzssyjlbBsInfo::getGantzl31, bo.getGantzl31());
        lqw.eq(bo.getHansl31() != null, ReportJlKfnewsxzssyjlbBsInfo::getHansl31, bo.getHansl31());
        lqw.eq(StringUtils.isNotBlank(bo.getHeh32()), ReportJlKfnewsxzssyjlbBsInfo::getHeh32, bo.getHeh32());
        lqw.eq(bo.getHezl32() != null, ReportJlKfnewsxzssyjlbBsInfo::getHezl32, bo.getHezl32());
        lqw.eq(bo.getHejstzl32() != null, ReportJlKfnewsxzssyjlbBsInfo::getHejstzl32, bo.getHejstzl32());
        lqw.eq(bo.getHejgtzl32() != null, ReportJlKfnewsxzssyjlbBsInfo::getHejgtzl32, bo.getHejgtzl32());
        lqw.eq(bo.getShuifzl32() != null, ReportJlKfnewsxzssyjlbBsInfo::getShuifzl32, bo.getShuifzl32());
        lqw.eq(bo.getGantzl32() != null, ReportJlKfnewsxzssyjlbBsInfo::getGantzl32, bo.getGantzl32());
        lqw.eq(bo.getHansl32() != null, ReportJlKfnewsxzssyjlbBsInfo::getHansl32, bo.getHansl32());
        lqw.eq(bo.getHanslpjz3() != null, ReportJlKfnewsxzssyjlbBsInfo::getHanslpjz3, bo.getHanslpjz3());
        lqw.eq(bo.getYex() != null, ReportJlKfnewsxzssyjlbBsInfo::getYex, bo.getYex());
        lqw.eq(bo.getSux() != null, ReportJlKfnewsxzssyjlbBsInfo::getSux, bo.getSux());
        lqw.eq(bo.getSuxzs() != null, ReportJlKfnewsxzssyjlbBsInfo::getSuxzs, bo.getSuxzs());
        lqw.eq(bo.getHeh11Id() != null, ReportJlKfnewsxzssyjlbBsInfo::getHeh11Id, bo.getHeh11Id());
        lqw.eq(bo.getHeh12Id() != null, ReportJlKfnewsxzssyjlbBsInfo::getHeh12Id, bo.getHeh12Id());
        lqw.eq(bo.getHeh21Id() != null, ReportJlKfnewsxzssyjlbBsInfo::getHeh21Id, bo.getHeh21Id());
        lqw.eq(bo.getHeh22Id() != null, ReportJlKfnewsxzssyjlbBsInfo::getHeh22Id, bo.getHeh22Id());
        lqw.eq(bo.getHeh31Id() != null, ReportJlKfnewsxzssyjlbBsInfo::getHeh31Id, bo.getHeh31Id());
        lqw.eq(bo.getHeh32Id() != null, ReportJlKfnewsxzssyjlbBsInfo::getHeh32Id, bo.getHeh32Id());
        return lqw;
    }

    /**
     * 新增矿粉塑性指数试验检测记录
     */
    @Override
    public Boolean insertByBo(ReportJlKfnewsxzssyjlbBsInfoBo bo) {
        ReportJlKfnewsxzssyjlbBsInfo add = BeanUtil.toBean(bo, ReportJlKfnewsxzssyjlbBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
        }
        return flag;
    }

    /**
     * 修改矿粉塑性指数试验检测记录
     */
    @Override
    public Boolean updateByBo(ReportJlKfnewsxzssyjlbBsInfoBo bo) {
        ReportJlKfnewsxzssyjlbBsInfo update = BeanUtil.toBean(bo, ReportJlKfnewsxzssyjlbBsInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportJlKfnewsxzssyjlbBsInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除矿粉塑性指数试验检测记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 获取液限坐标点
     *
     * @param bo
     * @return
     */
    @Override
    public YexVo getYex(ReportJlKfnewsxzssyjlbBsInfoBo bo) {
//        LineFunction lineParam = LineUtil.getGeneralLine(data);
        YexVo yexVo = new YexVo();
//        yexVo.setYex(BigDecimalUtil.stpTrailZero(BigDecimalUtil.divide(
//            BigDecimalUtil.subtract(new BigDecimal(20), lineParam.getB(), 2),
//            lineParam.getK(), 2
//        )));
        if (BigDecimalUtil.isNotEmpty(bo.getHanslpjz3()) && BigDecimalUtil.isNotEmpty(bo.getPingjh3()) && BigDecimalUtil.isNotEmpty(bo.getPingjh2())
            && BigDecimalUtil.isNotEmpty(bo.getHanslpjz2()) && BigDecimalUtil.isNotEmpty(bo.getHanslpjz1()) && BigDecimalUtil.isNotEmpty(bo.getPingjh1())) {
            double G22 = bo.getHanslpjz3().doubleValue();
            double G14 = bo.getPingjh3().doubleValue();
            double E22 = bo.getHanslpjz2().doubleValue();
            double C22 = bo.getHanslpjz1().doubleValue();
            double E14 = bo.getPingjh2().doubleValue();
            double C14 = bo.getPingjh1().doubleValue();
            double T13 = G22 / (0.524 * G22 - 7.606);

            double T14 = Math.pow(10, (log10(G22) + ((log10(G22) - log10(E22)) * (log10(T13) - log10(G14))) / (log10(G14) - log10(E14))));
            double T15 = Math.pow(10, (log10(G22) + ((log10(G22) - log10(C22)) * (log10(T13) - log10(G14))) / (log10(G14) - log10(C14))));
            double T17 = (T14 + T15) / 2;

            double WL = Math.pow(10, (log10(G22) + ((log10(G22) - log10(T17)) * (log10(20) - log10(G14))) / (log10(G14) - log10(T13))));
            yexVo.setYex(BigDecimalUtil.defaultValue(WL, 1));
            double T19 = WL/(0.524*WL - 7.606);
            double WP = Math.pow(10, (log10(G22) + ((log10(G22) - log10(T17)) * (log10(T19) - log10(G14))) / (log10(G14) - log10(T19))));
            yexVo.setSux(BigDecimalUtil.defaultValue(WP, 1));
            double Ip = WL - WP;
            yexVo.setSuxzs(BigDecimalUtil.defaultValue(Ip, 1));
        }

        return yexVo;
    }
}
